using UnityEngine;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// 任务状态：刚刚接受并开始任务、正在进行未完成任务、已完成任务要求未交付、已交付任务并获得奖励
/// </summary>
[System.Serializable]
public enum QuestState
{
    Start, Progress, Complete, Finish
}
[CreateAssetMenu(fileName = "QuestData", menuName = "Quest/QuestData")]
public class QuestData_SO : ScriptableObject
{
    /// <summary>
    /// 任务需求
    /// </summary>
    [System.Serializable]
    public class QuestRequire
    {
        /// <summary>
        /// 敌怪名字或物品名字
        /// </summary>
        public string name;
        /// <summary>
        /// 任务需求数量
        /// </summary>
        public int requireAmount;
        /// <summary>
        /// 当前已完成数量
        /// </summary>
        public int currentAmount;
    }
    public string questName;
    [TextArea]
    public string questDescription;
    public QuestState questState;
    /// <summary>
    /// 记录当前任务的任务需求
    /// </summary>
    public List<QuestRequire> questRequires = new List<QuestRequire>();
    /// <summary>
    /// 任务奖励
    /// </summary>
    public List<InventoryItem> rewards = new List<InventoryItem>();
    /// <summary>
    /// 检查任务完成情况
    /// </summary>
    public void CheckQuestProgress()
    {
        // 找任务中的所有 require 中的 0 / 3 判断是否整个任务已完成
        // 已完成的 require 的数量 == 任务的 require 总数
        // IEnumrable 相当于没有索引的数组，能 forEach 循环但不能 for 循环
        var finishRequires = questRequires.Where(r => r.requireAmount <= r.currentAmount);
        if (finishRequires.Count() == questRequires.Count)
        {
            // 任务完成
            questState = QuestState.Complete;
        }
        else
        {
            questState = QuestState.Progress;
        }
    }
    // 获取当前任务需求的所有 物品/敌怪 的名字列表
    public List<string> GetRequireNameList()
    {
        List<string> requireNameList = new List<string>();
        foreach (var require in questRequires)
        {
            requireNameList.Add(require.name);
        }
        return requireNameList;
    }
    public void GiveRewards()
    {
        foreach (var reward in rewards)
        {
            // 需要增加物品
            if (reward.inventoryAmount > 0)
            {
                InventoryManager.Instance.inventoryData.AddItem(reward.itemData, reward.inventoryAmount);
            }
            // 需要扣除物品
            else if (reward.inventoryAmount < 0)
            {
                int requireAmount = Mathf.Abs(reward.inventoryAmount);
                // 背包中有需要扣除的物品
                if (InventoryManager.Instance.QuestItemInBag(reward.itemData) != null)
                {
                    // 背包中物品不足或恰好够
                    if (InventoryManager.Instance.QuestItemInBag(reward.itemData).inventoryAmount <= requireAmount)
                    {
                        // 先从背包栏减去需要扣除的物品
                        requireAmount -= InventoryManager.Instance.QuestItemInBag(reward.itemData).inventoryAmount;
                        InventoryManager.Instance.QuestItemInBag(reward.itemData).inventoryAmount = 0;
                        // 从快捷栏减去剩余需要扣除的物品
                        if (InventoryManager.Instance.QuestItemInAction(reward.itemData) != null)
                        {
                            InventoryManager.Instance.QuestItemInAction(reward.itemData).inventoryAmount -= requireAmount;
                        }
                    }
                    // 背包中物品完全足够
                    else
                    {
                        InventoryManager.Instance.QuestItemInBag(reward.itemData).inventoryAmount -= requireAmount;
                    }
                }
                // 快捷栏有需要扣除的物品
                else
                {
                    InventoryManager.Instance.QuestItemInAction(reward.itemData).inventoryAmount -= requireAmount;
                }
            }
            InventoryManager.Instance.inventoryUI.RefreshUI();
            InventoryManager.Instance.actionUI.RefreshUI();
        }

    }
}